package cn.zy.company.servlet.admin;


import cn.zy.company.entity.News;
import cn.zy.company.entity.User;
import cn.zy.company.service.NewsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/admin/addNews")
public class AddNewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/jsp/admin/addNews.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            User loginUser = (User) req.getSession().getAttribute("loginUser");
            Integer userId = loginUser.getId();
            String name = req.getParameter("name");
            System.out.println(name);
            String value = req.getParameter("value");
            News news = new News(name, value, userId, null);
            news.setDate(new Date());
            NewsService newsService = new NewsService();
            newsService.add(news);
            resp.sendRedirect("showNews");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
